package com.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


import com.clases.base.Boleto;
import com.dominio.Usuario;
import com.mysql.jdbc.Driver;

public class BDManagement {
	
	private final static String drv = "com.mysql.jdbc.Driver";
	private final static String db = "jdbc:mysql://localhost:9999/bdatos";
	private final static String userAndPass = "root";
	private PreparedStatement pgetAllBoleto = null;
	private PreparedStatement pgetAllUsuarios = null;
	private PreparedStatement pgetAllBoletoById = null;
	private PreparedStatement psetBoleto = null;
	private PreparedStatement psetUser = null;
	private Connection ct;
	private Statement st;
	
	
	public BDManagement() {
		try {
			Class.forName(drv);
			ct = DriverManager.getConnection(db,userAndPass,userAndPass);
			pgetAllBoleto = ct.prepareStatement("select * from boletos");
			pgetAllUsuarios = ct.prepareStatement("select * from usuarios");
			//pgetAllBoletoById = ct.prepareStatement("select * from boletos b where b.id = ?");
			psetBoleto = ct.prepareStatement("INSERT INTO boletos (idBoleto,fechaViaje,fechaActual,asiento,partida,destino,horario,boleteria)  VALUE  (?,?,?,?,?,?,?,?)");
			psetUser = ct.prepareStatement("INSERT INTO usuarios (ciudad,user,pass,email,tel,puesto) VALUE (?,?,?,?,?,?)");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public Statement getSt(){
		return st;
	}
	
	
	public void setUser(Usuario u){
		try{
			
			psetUser.setNString(1, u.getCiudad());
			psetUser.setNString(2, u.getNick());
			psetUser.setNString(3, u.getPassword());
			psetUser.setNString(4, u.getEmail());
			psetUser.setNString(5, u.getTel());
			psetUser.setNString(6, u.getPuesto());
			psetUser.executeUpdate();
		}catch(SQLException e){
			e.printStackTrace();
		}
		
	}
	
	public ResultSet getAllUsuarios(){
		try{
			return pgetAllUsuarios.executeQuery();
		}catch(SQLException e){
			e.printStackTrace();
		}
		return null;
	}
	
	public ResultSet getAllBoletos(){
		
		try {
			return pgetAllBoleto.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;		
		
	}
	
	public ResultSet getAllBoletoById(int id){
		
		try {
			pgetAllBoletoById.setInt(1,id);
			return pgetAllBoletoById.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
		
	}
	
	public void setBoleto(Boleto b){
		try {
			psetBoleto.setInt(1, b.getIdBoleto());
			psetBoleto.setNString(2, b.getFechaViaje());
			psetBoleto.setDate(3, b.getFechaActual());
			psetBoleto.setInt(4, b.getAsiento());
			psetBoleto.setNString(5, b.getPartida());
			psetBoleto.setNString(6, b.getDestino());
			psetBoleto.setNString(7, b.getHorario());
			psetBoleto.setNString(8, b.getBoleteria());
			psetBoleto.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	
	
	
	
	
	

}
